package com.codejam2012.round1c;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class A {

	static int nTest;
	static Scanner scanner;
	static FileWriter writer;
	static File in;
	static File out;
	
	static int nVert;
	static int[] inDeg;
	static int[] outDeg;
	static int[][] sup;
	static int[] label;
	static int[] pre;
	static boolean hasDiamond;
	
	public static void main(String[] args) throws IOException  {
		in = new File("input.txt");
		out = new File("output.txt");
		scanner = new Scanner(in);
		writer = new FileWriter(out);
		
		nTest = scanner.nextInt();
		for (int test=1;test<=nTest;++test) {
			nVert = scanner.nextInt();
			outDeg = new int[nVert+1];
			inDeg = new int[nVert+1];
			sup = new int[nVert+1][];
			label = new int[nVert+1];
			pre = new int[nVert+1];
			hasDiamond = false;
			for (int i=1;i<=nVert;++i) {
				inDeg[i] = 0;
				label[i] = 0;
				pre[i] = 0;
			}
			for (int i=1;i<=nVert;++i) {
				outDeg[i] = scanner.nextInt();
				sup[i] = new int[outDeg[i]+1];
				for (int j=1;j<=outDeg[i];++j) {
					sup[i][j] = scanner.nextInt();
					inDeg[sup[i][j]]++;
				}
			}
			
			for (int i=1;i<=nVert;++i) {
				if (hasDiamond) {
					break;
				}
				if (inDeg[i] == 0 && label[i] == 0) {
					dfs(i, i);
				}
			}
			writer.write("Case #" + test + ": ");
			if (hasDiamond) {
				writer.write("Yes");
			}
			else {
				writer.write("No");
			}
			if (test < nTest) {
				writer.write("\n");
			}
			writer.flush();
		}
		writer.close();
	}

	private static void dfs(int n, int group) {
		// TODO Auto-generated method stub
		if (hasDiamond) {
			return;
		}
		label[n] = group;
		for (int i=1;i<=outDeg[n];++i) {
			int k = sup[n][i];
			if (label[k] != group) {
				dfs(k, group);
			}
			else if (label[k] == group) {
				hasDiamond = true;
			}
		}
	}
}
